package com.vnp.cooking.fragmenthome;

import java.util.ArrayList;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockListFragment;
import com.vnp.cooking.DetailBoSuuTapActivity;
import com.vnp.cooking.R;
import com.vnp.cooking.adapter.ListThongbaoAdapter;
import com.vnp.cooking.adapter.ThemBoSuuTapAdapter;
import com.vnp.cooking.bean.ObjBST;
import com.vnp.cooking.bean.ObjThongbao;
import com.vnp.cooking.httpclient.HttpClientHelper;
import com.vnp.cooking.httpclient.MyJsonHttpResponseHandler;
import com.vnp.cooking.utils.KeyboardHelper;
import com.vnp.cooking.utils.PreferenceHelper;
import com.vnp.cooking.utils.Utils;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class FrgBosuutap extends SherlockListFragment{
	View v;
	ThemBoSuuTapAdapter adapter;
	ArrayList<ObjBST> objBSTs = new ArrayList<ObjBST>();
	PreferenceHelper preferenceHelper;
	ProgressBar progressBar;
	TextView noBST;
	TextView title;
	Handler mHandler = new Handler();
	
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			getBST();
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.frg_thongbao, container, false);
		preferenceHelper = new PreferenceHelper(getActivity());
		
		
		progressBar = (ProgressBar) v.findViewById(R.id.progressBarBST);
		noBST = (TextView) v.findViewById(R.id.no_bst);
		title = (TextView) v.findViewById(R.id.txt_title_bst);
		
		Log.i("DATA", "Cache bosutap: "+preferenceHelper.getCacheBosuutap());
		if (preferenceHelper.getCacheBosuutap().equals("")) {
			mHandler.removeCallbacks(r);
			mHandler.postDelayed(r, 200);
		} else {
			loadCache();
		}
		
		setFont();
		return v;
	}
	
	void setFont() {
		title.setTypeface(Utils.getTypefaceRegular(getActivity()),Typeface.BOLD);
	}
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		KeyboardHelper.hideSoftKeyboard(getActivity());
		init();
		adapter = new ThemBoSuuTapAdapter(getActivity(), objBSTs);
		getListView().setAdapter(adapter);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		KeyboardHelper.hideSoftKeyboard(getActivity());
	}
	void init() {
		
		
	}
	
	
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Intent t = new Intent(getActivity(), DetailBoSuuTapActivity.class);
		t.putExtra("collection_id", objBSTs.get(position).getCollection_id());
		t.putExtra("nameBST", objBSTs.get(position).getName());
		Log.i("DATA", "CollectionID truyen sang: "+objBSTs.get(position).getCollection_id());
		getActivity().startActivity(t);
		getActivity().overridePendingTransition(R.anim.open_next, R.anim.close_main);
	}

	
	void getBST() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
				hiddenProgessDialog();
				noBST.setVisibility(View.VISIBLE);
				noBST.setText("Không lấy được bộ sưu tập");
			}

			@Override
			public void onSuccess(JSONObject re) {
				hiddenProgessDialog();
				if (!re.toString().equals("")) {
					preferenceHelper.setCacheBosuutap(re.toString());
				}
				parserData(re);
			}
			
		});
		httpClientHelper.getBST(preferenceHelper.getID());
	}
	
	public void parserData(JSONObject re) {
		try {
			int status = re.getInt("status");
			if (status == 200) {
				hiddenProgessDialog();
				JSONArray data = re.getJSONArray("data");
				Log.i("DATA", data.toString());
				if (data.length() == 0) {
					noBST.setVisibility(View.VISIBLE);
				}
				objBSTs.clear();
				for (int i = 0; i < data.length(); i++) {
					JSONObject collection = data.getJSONObject(i);
					ObjBST objBST = new ObjBST();
					objBST.setCollection_id(collection.getString("collection_id"));
					objBST.setName(collection.getString("name"));
					objBST.setSoluong(collection.getInt("total_items"));
					
					objBSTs.add(objBST);
				}
				adapter.notifyDataSetChanged();
				
			} else {
				hiddenProgessDialog();
				noBST.setVisibility(View.VISIBLE);
				noBST.setText("Không lấy được bộ sưu tập");
			}
		} catch (JSONException e) {
			e.printStackTrace();
			hiddenProgessDialog();
			noBST.setVisibility(View.VISIBLE);
			noBST.setText("Không lấy được bộ sưu tập");
		}
	}
	
	
	void loadCache() {
		String cache = preferenceHelper.getCacheBosuutap();
		if(!cache.equals("")) {
			try {
				Log.i("DATA", "parse cache======");
				JSONObject response = new JSONObject(cache);
				parserData(response);
			} catch (Exception e) { }
		}
	}
	
	// hide progress bar
	void hiddenProgessDialog() {
		try {
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try {
						progressBar.setVisibility(View.GONE);
					} catch (Exception e) {
					}
				}
			});
		} catch (Exception e) {
		}
	}

}
